import { PageConfig } from 'next';
import anylogger from 'anylogger';
import '@mmstudio/an000042';
import an48 from '@mmstudio/an000048';
import getuser from '../../../../../atoms/getuser';
// import { tbchoosecourses } from '../../../../../atoms/db';

const logger = anylogger('pages/api/teachjd/xsxk/content/query.api');

// export type IData = Pick<ITbchoosecourses, 'coursename' | 'classname'>
export type IData = unknown;

export type Result = {
	count: number;
	data: unknown[];
	// data: IData[];
};

export type Query = {
	q: string
	page: string;
	pagesize: string;
}

/**
 * 查询服务
 */
const handler = an48<Result>();

handler.get(async (req, res) => {
	// todo need fixed
	// try {
	// 	logger.debug('msg query:', req.query);
	// 	const { q, ...rest } = req.query as Query;
	// 	const page = Number(rest.page || '1');
	// 	const pagesize = Number(rest.pagesize || '10');
	// 	const user = await getuser(req);
	// 	if (!user || user.role !== 'admin') {
	// 		res.status(200).json({
	// 			count: 0,
	// 			data: []
	// 		}); return;
	// 	}
	// 	const dt1 = tbchoosecourses();
	// 	if (q) {
	// 		const query = q.trim();
	// 		dt1.where('coursename', 'like', `%${query}%`)
	// 			.orWhere('classname', 'like', `%${query}%`);
	// 	}
	// 	const [{ size }] = await tbchoosecourses().count('*', { as: 'size' });
	// 	const count = Number(size);
	// 	const dt2 = tbchoosecourses();
	// 	if (q) {
	// 		const query = q.trim();
	// 		dt2.where('coursename', 'like', `%${query}%`)
	// 			.orWhere('classname', 'like', `%${query}%`);
	// 	}
	// 	const offset = (page - 1) * pagesize;
	// 	const d = await dt2
	// 		.select('classname', 'coursename', 'coursegroup', 'coursetype', 'credit', 'teachingsemester')
	// 		.groupBy('classname', 'coursename', 'coursegroup', 'coursetype', 'credit', 'teachingsemester')
	// 		.limit(pagesize)
	// 		.offset(offset)
	// 		.orderBy('coursename', 'asc');
	// 	const data = d.map((it) => {
	// 		return {
	// 			...it
	// 		} as IData;
	// 	});
	// 	res.status(200).json({ data, count });
	// } catch (error) {
	// 	logger.trace(error);
	// 	res.status(200).json({
	// 		count: 0,
	// 		data: []
	// 	});
	// }
});

export const config = {} as PageConfig;

export default handler;
